package jafer.tela;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

public class Nave {

	private int x, y;
	private int dx, dy;
	private Image imagem;
	private boolean isVisible;

	private List<Missil> misseis;

	private int altura, largura;

	public Nave() {
		ImageIcon referencia = new ImageIcon("res/nave.gif");
		imagem = referencia.getImage();

		altura = imagem.getHeight(null);
		largura = imagem.getWidth(null);

		misseis = new ArrayList<Missil>();

		this.x = 100;
		this.y = 100;
	}

	public void mexer() {
		
		System.out.println(x+","+y);

		x += dx;// -2,598
		y += dy;// -1, 427

		if (x < -2)
			x = -2;
		if (x > 470)
			x = 470;
		if (y < -1)
			y = -1;
		if (y > 344)
			y = 344;
	}

	public List<Missil> getMisseis() {
		return misseis;
	}

	public boolean isVisible() {
		return isVisible;
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public void atira() {
		misseis.add(new Missil(x + largura, y + altura / 2));
	}

	public void keyPressed(KeyEvent tecla) {
		int codigo = tecla.getKeyCode();

		if (codigo == KeyEvent.VK_SPACE)
			atira();

		if (codigo == KeyEvent.VK_UP)
			dy = -1;

		if (codigo == KeyEvent.VK_DOWN)
			dy = 1;

		if (codigo == KeyEvent.VK_LEFT)
			dx = -1;

		if (codigo == KeyEvent.VK_RIGHT)
			dx = 1;

	}

	public void keyReleased(KeyEvent tecla) {
		int codigo = tecla.getKeyCode();
		if (codigo == KeyEvent.VK_UP) {
			dy = 0;
		}
		if (codigo == KeyEvent.VK_DOWN) {
			dy = 0;
		}
		if (codigo == KeyEvent.VK_LEFT) {
			dx = 0;
		}
		if (codigo == KeyEvent.VK_RIGHT) {
			dx = 0;
		}

	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Image getImagem() {
		return imagem;
	}

	public Rectangle getBouds() {
		return new Rectangle(x, y, largura, altura);
	}

}
